const fs = require('fs')
const path = require("path")
const { removeSync, copySync } = require('fs-extra')
const chalk = require('react-dev-utils/chalk')
const { lookDir, PublicExcludes, SrcPagesExcludes} = require('./look-dir')

const PublicPath = path.resolve(__dirname, '..', '..','public')
const srcPages = path.resolve(__dirname, '..', '..', 'src', 'pages')
// console.log('PublicPath=', PublicPath)

function cleanPublicHtml() {
  lookDir(PublicPath, PublicExcludes, (fullPath, fileInfo, file) => {
    removeSync(fullPath)
  })
}

function publicHtmlWithSrc() {
  lookDir(srcPages, SrcPagesExcludes, (fullPath, fileInfo, file) => {
    const fileNE = file + '.html'
    const sourcePath = path.resolve(fullPath, fileNE)
    const targetPath = path.resolve(PublicPath, fileNE)
    if (!fileInfo.isDirectory()) {
      return
    // } else if (file !== 'index') {
    } else {
      const subFileInfo = fs.statSync(sourcePath)
      if (!(subFileInfo && subFileInfo.isFile())) {
        console.log('')
        console.log(chalk.red('目标文件不存在,', fileNE, ' -- in=', sourcePath))
        console.log('')
        return
      }
    }
    copySync(sourcePath, targetPath)
  })

  // copySync(path.resolve(srcPages, 'index', 'index.html'), path.resolve(PublicPath, 'index.html'))
}


cleanPublicHtml()
publicHtmlWithSrc()
